package com.banciyuan.bcywebview.biz.main.newdiscover.ranklist;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.RankItem;


public class InterestRankActivity extends BaseActivity {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<RankItem.RankInfo> mRankInfos;
    private List<Fragment> flist = new ArrayList<>();
    private String name;
    private int position = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.interest_rank_layout);
        initArgs();
        initUi();
        initActionbar();
        initAction();
        initData();
    }

    @Override
    protected void initArgs() {
        mRankInfos = (List<RankItem.RankInfo>) getIntent().getSerializableExtra(gotoUtil.INTENT_VALUE_ONE);
        name = getIntent().getStringExtra(gotoUtil.INTENT_VALUE_TWO);
        if (null != mRankInfos && !TextUtils.isEmpty(name)) {
            for (int i = 0; i < mRankInfos.size(); i++) {
                if (name.equals(mRankInfos.get(i).getName())) {
                    position = i;
                    break;
                }
            }
        }
    }

    @Override
    protected void initActionbar() {
        View mActionBarView = findViewById(R.id.base_action_bar);
        ActionbarHelper mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.interest_rank));
    }

    @Override
    protected void initUi() {
        mTabLayout = (TabLayout) findViewById(R.id.tablayout);
        mViewPager = (ViewPager) findViewById(R.id.mViewpager);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }

    private void initTabAndViewPager() {
        mViewPager.setOffscreenPageLimit(mRankInfos.size());
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return flist.get(position);
            }

            @Override
            public int getCount() {
                return mRankInfos.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mRankInfos.get(position).getName();
            }
        });
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setCurrentItem(position);
    }

    @Override
    protected void initData() {
        if (null == mRankInfos)
            return;
        getFragments();
        initTabAndViewPager();

    }

    private void getFragments() {
        for (int i = 0; i < mRankInfos.size(); i++) {
            InterestRankFragment interestRankFragment = new InterestRankFragment();
            Bundle bundle = new Bundle();
            bundle.putString(gotoUtil.INTENT_VALUE_ONE, mRankInfos.get(i).getType());
            interestRankFragment.setArguments(bundle);
            flist.add(interestRankFragment);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }
}
